layui.use(['form', 'layer', 'laydate'], function () {
    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        tnames = [],
        currentOption = '',
        searchStr = '',
        newTNames = [],
        uniqueNewTNames = [],
        that = null,
        company_id = sessionStorage["company_id"];
    laydate.render({
        elem: '.signUpYear',
        format: 'yyyy年',
        type: 'year'
    });
    form.on("submit(editStudent)", function (data) {
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候', { icon: 16, time: false, shade: 0.8 });
        // 实际使用时的提交信息
        const { studentId: id, tname, educationType, cengci, school, major, name, phone, cardid, mail, sosperson, sosphone, remarks, money, oweafee, chengben, unifiedExam, paper, baccalaureate, signUpTime, signUpYear, veteran } = data.field;
        var student = {
            id,
            tid: tname,
            educationType: educationType,
            cengci: cengci,
            school: school,
            major: major,
            name: name,
            phone: phone,
            cardid: cardid,
            mail: mail,
            sosperson: sosperson,
            sosphone: sosphone,
            remarks: remarks,
            money: money,
            oweafee: oweafee,
            chengben: chengben,
            unifiedExam: unifiedExam ? 1 : 0,
            paper: paper ? 1 : 0,
            baccalaureate: baccalaureate ? 1 : 0,
            signUpTime: signUpTime,
            signUpYear: signUpYear,
            veteran: veteran,
            company_id
        };
        if (data.field.addOrUpdate == 0) {//添加学员信息
            student.creattime = getTime();
            $.ajax({
                url: baseUrl + "student/addStudentInfo.php",
                data: student,
                type: "POST",
                async: false,
                dataType: "text",
                success: function (data) {
                    if (data == "1") {
                        message(index, '添加学员信息成功！')
                    } else {
                        message(index, '添加学员信息失败！')
                    }
                }
            })
        } else {//修改学员信息
            $.ajax({
                url: baseUrl + "student/updateStudentInfo.php",
                data: student,
                type: "POST",
                dataType: "text",
                success: function (data) {
                    if (data == "1") {
                        message(index, '修改学员信息成功！')
                    } else {
                        message(index, '修改学员信息失败！')
                    }
                }
            })
        }
        return false;
    })
    if (sessionStorage["scale"] == 1) {
        $("#chengben").css({ "display": "block" })
        $(".tname").removeAttr('disabled')
        $(".searchTName").css({ "display": "block" })
    } else if (sessionStorage["scale"] == 2) {
        $(".tname").removeAttr('disabled')
        $(".searchTName").css({ "display": "block" })
    }

    setTimeout(function () {
        form.render('select')
        currentOption = $('.tname').val()
        $('.tname option').each(function (index, item) {
            if (index != 0) tnames.push({ tname: item.text, val: item.value })
        })
    }, 500)
    //在报名老师下拉框中检索
    $('.searchTName').on('keyup', function () {
        that = this
        //确保只进行一次检索
        clearTimeout(this.timer)
        this.timer = setTimeout(function () {
            searchStr = $(that).val()
            newTNames = []
            uniqueNewTNames = []
            $('.tname').empty()
            $('.tname').append(new Option('请选择', ''))
            if ($.trim(searchStr) != '') {
                tnames.forEach(function (item) {
                    if (item.tname.includes(searchStr)) {
                        newTNames.push(item)
                    }
                })
                uniqueNewTNames = unique(newTNames);
                uniqueNewTNames.forEach(function (item) {
                    $('.tname').append(new Option(item.tname, item.val))
                })
            } else {
                tnames.forEach(function (item) {
                    if (item.val == currentOption) {
                        $('.tname').append(new Option(item.tname, item.val, true, true))
                    } else {
                        $('.tname').append(new Option(item.tname, item.val))
                    }
                })
            }
            form.render('select')
        }, 500)
    })

    function message(index, msg) {
        setTimeout(function () {
            top.layer.close(index);
            top.layer.msg(msg);
            parent.layer.closeAll("iframe");
            //父页面表格重载
            parent.layui.table.reload("studentListTable");
        }, 1000);
    }

    //数组去重 支持复杂类型
    function unique(arr) {
        let obj = {}
        return arr.filter((item, index) => {
            // 防止key重复
            let newItem = item + JSON.stringify(item)
            return obj.hasOwnProperty(newItem) ? false : obj[newItem] = true
        })
    }
})